<?php

/**
 * This is the model class for table "{{union}}".
 *
 * The followings are the available columns in table '{{union}}':
 * @property integer $id
 * @property string $name
 * @property string $address
 * @property string $phone_number
 * @property string $president
 * @property string $allowance
 * @property string $management_fee
 * @property string $signing_contract_date
 * @property string $dispatched_company
 * @property string $create_date
 * @property string $created_by
 * @property string $update_date
 * @property string $updated_by
 */
class Union extends CActiveRecord
{
	public $keyword;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Union the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{union}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array ('name','required', 'message' => t ( 'MSG_REQUIRED', 'messages' )),
			array('dispatched_company, name, address', 'length', 'max'=>100, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('phone_number', 'length', 'max'=>200, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('president', 'length', 'max'=>30, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('allowance, management_fee, created_by, updated_by', 'length', 'max'=>45, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('signing_contract_date, keyword, create_date, update_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('dispatched_company, signing_contract_date, keyword, allowance,management_fee, id, name, address, phone_number, president, create_date, created_by, update_date, updated_by', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'hiringCompany' => array (
					self::HAS_MANY,
					'HiringCompany',
					'union_id'
			)
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => t('LBL_ID', 'union'),
			'name' => t('LBL_NAME', 'union'),
			'address' => t('LBL_ADDRESS', 'union'),
			'phone_number' => t('LBL_PHONE_NUMBER', 'union'),
			'president' => t('LBL_PRESIDENT', 'union'),
			'allowance' => t('LBL_ALLOWANCE', 'union'),
			'management_fee' => t('LBL_MANAGEMENT_FEE', 'union'),
			'signing_contract_date' => t('LBL_SIGNING_CONTRACT_DATE', 'union'),
			'dispatched_company' => t('LBL_DISPATCHED_COMPANY', 'union'),
			'create_date' => t('LBL_CREATE_DATE', 'union'),
			'created_by' => t('LBL_CREATED_BY', 'union'),
			'update_date' => t('LBL_UPDATE_DATE', 'union'),
			'updated_by' => t('LBL_UPDATED_BY', 'union'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		
		$criteria->addSearchCondition('name', $this->keyword, true);
		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('phone_number',$this->phone_number,true);
		$criteria->compare('president',$this->president,true);
		$criteria->compare('allowance',$this->allowance,true);
		$criteria->compare('management_fee',$this->management_fee,true);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('signing_contract_date',$this->signing_contract_date,true);
		$criteria->compare('dispatched_company',$this->dispatched_company,true);
		$criteria->compare('created_by',$this->created_by,true);
		$criteria->compare('update_date',$this->update_date,true);
		$criteria->compare('updated_by',$this->updated_by,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function beforeSave() {
		if (parent::beforeSave()) {
	
			$current_date = date(LGConstant::DATE_FORMAT_FOR_DATABASE);
	
			//Get user is logging
			$logedInUser = User::model()->findByPk(Yii::app()->user->id);
	
			if ($this->isNewRecord) {
				$this->create_date = $current_date;
				$this->created_by = $logedInUser->username;
			}
	
			$this->update_date = $current_date;
	
			if(isset($this->signing_contract_date) && $this->signing_contract_date != NULL){
				$date = DateTime::createFromFormat ( 'd-m-Y', $this->signing_contract_date );
				$this->signing_contract_date = $date->format ( 'Y-m-d' );
			}else{
				$this->signing_contract_date = NULL;
			}
			
			if (isset($logedInUser)) {
				$this->updated_by = $logedInUser->username;
			}
	
			$this->setIsNewRecord(false);
			return true;
		} else {
			return false;
		}
	}
	
	protected function afterFind() {
		if($this->signing_contract_date != NULL){
			$this->signing_contract_date = strtotime ( $this->signing_contract_date );
			$this->signing_contract_date = date ( LGConstant::DATE_FORMAT_NOT_TIME_MONTH_NUMBER, $this->signing_contract_date );
		}
	
		parent::afterFind ();
	}
	
	public function getChtmlDropdownData(){
		$listData = $this->findAll();
		return Html::listData($listData, 'id', 'name');
	}
	public function getChtmlDropdownDataByAllowance(){
		$listData = $this->findAll();
		return CHtml::listData($listData, 'id', 'allowance');
	}
	public function getChtmlDropdownDataByManagementFee(){
		$listData = $this->findAll();
		return CHtml::listData($listData, 'id', 'management_fee');
	}
}